# Maintainer: Anatol Pomozov <anatol pomozov at gmail>
# Contributor: Johannes Dewender  arch at JonnyJD dot net
# Contributor: Konrad <konrad AT knauber DOT name>
# Contributor: Luka Perkov <archlinux <at> lukaperkov <dOt> net>
# Contributor: Fernando M <f <at> beford.net>
# Author: Wintershade <Wintershade AT google mail DOT com>

pkgname=rpm-tools
pkgver=4.18.2
pkgrel=1
pkgdesc="RPM Package Manager - RPM.org fork, used in major RPM distros"
arch=('loong64' 'x86_64')
url='http://www.rpm.org/'
license=('GPL2')
depends=('lua' 'file' 'nss' 'popt' 'elfutils' 'libarchive' 'libcap')
makedepends=('python' 'pkg-config')
conflicts=('rpm' 'rpmextract')
options=('!libtool')
provides=("rpm=${pkgver}" 'rpmextract=1.0-4' 'rpm-org')
install=rpm-tools.install

_pkgver_major="${pkgver%%.*}"
_pkgver_major_rem="${pkgver#*.}"
_pkgver_minor="${_pkgver_major_rem%%.*}"
_base_pkgver=$_pkgver_major.$_pkgver_minor.x

source=(http://ftp.rpm.org/releases/rpm-$_base_pkgver/rpm-$pkgver.tar.bz2
	rpmextract.sh
        rpmlib-filesystem-check.patch
        rpm-add-loongarch.patch)
sha256sums=('ba7eee1bc2c6f83be73c0a40d159c625cbaed976b3ac044233404fb25ae1b979'
            '3e5bf450d4628366ba35469ec0530a99cd09ab2616a3d261a3f68270f481f777'
            'bd0e6dbd458f990268c60324190c6825b234647ecdde08296d2b453dc4bce27a'
            'a40afdd567b77480d2e315d3ca297f5238dd3f9f11cccc239a1d33b297b84e04')

prepare() {
	cd rpm-${pkgver}
	patch -p1 < ../rpmlib-filesystem-check.patch
    patch -p1 -i $srcdir/rpm-add-loongarch.patch
}

build() {
	cd rpm-${pkgver}

	./configure \
		--prefix=/usr  \
		--sysconfdir=/etc  \
		--localstatedir=/var \
		--enable-python \
		--enable-ndb \
		--enable-bdb-ro \
		--with-cap \
		CPPFLAGS="`pkg-config --cflags nss`" \
		PYTHON=python
	make
}

package() {
	cd rpm-${pkgver}
	make prefix=${pkgdir}/usr localstatedir=${pkgdir}/var install
	rmdir ${pkgdir}/var/tmp
	rmdir ${pkgdir}/var
	# rpmextract using bsdtar, needs libarchive
	install -m755 ${srcdir}/rpmextract.sh ${pkgdir}/usr/bin/

	# move rpm from /bin to /usr/bin
	rm ${pkgdir}/usr/bin/rpm{query,verify}
	cd ${pkgdir}/usr/bin
	ln -s rpm rpmquery
	ln -s rpm rpmverify

	# also install python 3 files
	# building with python 3 files as default doesn't seem to work
	cd ${srcdir}/rpm-${pkgver}
	cd python
	python setup.py install --root="$pkgdir/" --optimize=1
}
